package cn.donghuapian.config;

import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: hzw
 * @time: 2024/12/16 17:50
 */
@Configuration
public class CircuitBreakerConfigurer {

    @Bean
    public CircuitBreakerRegistry registry() {
        CircuitBreakerConfig c = CircuitBreakerConfig.custom()
                .failureRateThreshold(30.0f)
                .slowCallRateThreshold(30.0f)
                .slowCallDurationThreshold(Duration.ofSeconds(3))
                .permittedNumberOfCallsInHalfOpenState(5)
                .slidingWindowType(CircuitBreakerConfig.SlidingWindowType.COUNT_BASED)
                .slidingWindowSize(20)
                .minimumNumberOfCalls(20)
                .waitDurationInOpenState(Duration.ofSeconds(300))
                .build();

        Map<String, CircuitBreakerConfig> configs = new HashMap<>();
        configs.put("testConfig", c);

        CircuitBreakerRegistry registry = CircuitBreakerRegistry.of(configs);
        registry.circuitBreaker("breakName", "testConfig");

        return registry;
    }
}
